package models

import (
	"exp1/appConfig"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func Init() {

	var err error
	MysqlConfig := appConfig.Config.Mysql

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		MysqlConfig.User,
		MysqlConfig.Password,
		MysqlConfig.Host,
		MysqlConfig.Port,
		MysqlConfig.Database)
	fmt.Println("dsn:", dsn)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("mysql 连接成功")

	err = DB.AutoMigrate(&User{}, &UserRole{}, &Role{}, &RoleNode{}, &Node{}, &Comp{}, &Exp{}, &Orders{})
	if err != nil {
		panic(err)
	}
	fmt.Println("mysql 迁移表成功")

}
